package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.InsuranceService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

/**
 * 医保服务人员Controller
 * 
 * @author EX-LUORONG001
 * 
 */
@Controller
public class InsuranceController extends BaseRest {
	// private Log log = LogFactory.getLog(this.getClass());
	@BizServicesProxy
	private InsuranceService insuranceService;

	public void setContactService(InsuranceService insuranceService) {
		this.insuranceService = insuranceService;
	}

	/**
	 * 根据条件获取医保服务人员总数据条数
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/insurance/exportInsuranceCount")
	@ResponseBody
	public Map<String, Object> exportRulesCount(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			String providerid = request.getParameter("providerid");
			String name = request.getParameter("name");
			String personType = request.getParameter("personType");
			String departNo = request.getParameter("departNo");
			String type = request.getParameter("type");

			params.put("providerid", providerid);
			params.put("name", name);
			params.put("personType", personType);
			params.put("departNo", departNo);
			params.put("type", type);
			int total = this.insuranceService.queryContactCount(params);
			result.put("total", total);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医保服务人员新增
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/insurance/create")
	public Map<String, Object> addContact(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			String region = user.getAreaCode();
			transferJson.put("createdBy", user.getUsername());
			transferJson.put("updatedBy", user.getUsername());
			transferJson.put("region", region);
			result = this.insuranceService.addContact(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医保服务人员修改
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/insurance/update")
	public Map<String, Object> updateContact(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			transferJson.put("updatedBy", user.getUsername());
			String region = user.getAreaCode();
			transferJson.put("region", region);
			result = this.insuranceService.updateContact(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医保服务人员批量删除
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/insurance/delete")
	public Map<String, Object> deleteContact(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			transferJson.put("updatedBy", user.getUsername());
			result = this.insuranceService.deleteContact(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医保服务人员列表查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/insurance/page")
	public Map<String, Object> queryContactList(HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			String providerid = request.getParameter("providerid");
			String name = request.getParameter("name");
			String personType = request.getParameter("personType");
			String departNo = request.getParameter("departNo");
			String type = request.getParameter("type");
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("providerid", providerid);
			map.put("name", name);
			map.put("personType", personType);
			map.put("departNo", departNo);
			map.put("type", type);

			result = this.insuranceService
					.queryContactList(page, pageSize, map);
			result.put("provider",
					this.insuranceService.queryHealthDetail(providerid));
			result.put("authority", user.getRegionAuthority());
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医保服务人员详情查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/insurance/show")
	public Map<String, Object> queryContactDetail(HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			String id = request.getParameter("id");
			result = insuranceService.queryContactDetail(id);
			result.put("authority", user.getRegionAuthority());
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
}
